/*
 *    Copyright (c) 2018-2025, orient All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: orient
 */

package cn.goswan.orient.guohur.controller;

import cn.goswan.orient.common.security.util.SecurityUtils;
import cn.goswan.orient.guohur.entity.Goods;
import cn.goswan.orient.guohur.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.goswan.orient.common.core.util.R;
import cn.goswan.orient.common.log.annotation.SysLog;
import cn.goswan.orient.guohur.entity.GoodsSku;
import cn.goswan.orient.guohur.service.GoodsSkuService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * SKU
 *
 * @author wangchuang
 * @date 2020-06-01 09:30:01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/goodssku" )
@Api(value = "goodssku", tags = "SKU管理")
public class GoodsSkuController {

    private final  GoodsSkuService goodsSkuService;

    private final GoodsService goodsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param goodsSku SKU
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getGoodsSkuPage(Page page, GoodsSku goodsSku) {
        return R.ok(goodsSkuService.page(page, Wrappers.query(goodsSku)));
    }

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param goodsSku SKU
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/pageformerchant" )
    public R getGoodsSkuPageToMerchant(Page page,GoodsSku goodsSku){
		QueryWrapper<GoodsSku> queryWrapper = new QueryWrapper();
		queryWrapper.select(GoodsSku.class, info ->
				   !info.getColumn().equals("plateform_price")
		);
		queryWrapper.eq("shop_code", SecurityUtils.getUser().getUsername());
		if(goodsSku!=null){
			if(goodsSku.getGoodsName()!=null && !goodsSku.getGoodsName().isEmpty()){
				queryWrapper.like("goods_name",goodsSku.getGoodsName());
			}
			if(goodsSku.getGoodsTypeOne()!=null && !goodsSku.getGoodsTypeOne().isEmpty()){
				queryWrapper.eq("goods_type_one",goodsSku.getGoodsTypeOne());
			}
		}

		return R.ok(goodsSkuService.page(page, queryWrapper));
	}

    /**
     * 通过id查询SKU
     * @param skuCode id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{skuCode}" )
    public R getById(@PathVariable("skuCode" ) String skuCode) {
        return R.ok(goodsSkuService.getById(skuCode));
    }

    /**
     * 新增SKU
     * @param goodsSku SKU
     * @return R
     */
    @ApiOperation(value = "新增SKU", notes = "新增SKU")
    @SysLog("新增SKU" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('guohur_goodssku_add')" )
    public R save(@RequestBody GoodsSku goodsSku) {
    	goodsSku.setSkuCode("sku-"+System.currentTimeMillis());
    	goodsSku.setShopCode(SecurityUtils.getUser().getUsername());
    	goodsSku.setShopCode(SecurityUtils.getUser().getUsername());
        goodsSku.setInsP(SecurityUtils.getUser().getUsername());
        goodsSku.setInsT(LocalDateTime.now());
    	return R.ok(goodsSkuService.save(goodsSku));
    }

    /**
     * 修改SKU
     * @param goodsSku SKU
     * @return R
     */
    @ApiOperation(value = "修改SKU", notes = "修改SKU")
    @SysLog("修改SKU" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('guohur_goodssku_edit')" )
    public R updateById(@RequestBody GoodsSku goodsSku) {

		goodsSku.setUpdP(SecurityUtils.getUser().getUsername());
		goodsSku.setUpdT(LocalDateTime.now());
    	return R.ok(goodsSkuService.updateById(goodsSku));
    }

    /**
     * 通过id删除SKU
     * @param skuCode id
     * @return R
     */
    @ApiOperation(value = "通过id删除SKU", notes = "通过id删除SKU")
    @SysLog("通过id删除SKU" )
    @DeleteMapping("/{skuCode}" )
    @PreAuthorize("@pms.hasPermission('guohur_goodssku_del')" )
    public R removeById(@PathVariable String skuCode) {
        return R.ok(goodsSkuService.removeById(skuCode));
    }


	/**
	 * 导入商品至SKU
	 * @param  goodsCode 商品编号字符串
	 * @return R
	 */
	@ApiOperation(value = "导入商品", notes = "导入商品")
	@SysLog("导入商品" )
	@PostMapping("/goodsimport")
	@PreAuthorize("@pms.hasPermission('guohur_goodssku_add')" )
	public R goodsimport(@RequestParam(value = "goodscode")String goodsCode) {
		String [] codesArr = goodsCode.split(",");
		QueryWrapper<Goods> queryWrapper = new QueryWrapper();
		queryWrapper.in("goods_code",codesArr);
		//将字符串分割 查询出相应的商品记录
		List<Goods> goodsList = goodsService.list(queryWrapper);

		if(goodsList!=null && !goodsList.isEmpty()){
			List<GoodsSku> skuList = new ArrayList<>();
			for(Goods goods:goodsList){
				GoodsSku sku = new GoodsSku();
				sku.setSkuCode(UUID.randomUUID().toString());
				sku.setShopCode(SecurityUtils.getUser().getUsername());
				sku.setGoodsTypeOne(goods.getGoodsType());
				sku.setInsT(LocalDateTime.now());
				sku.setInsP(SecurityUtils.getUser().getUsername());
				sku.setSmartCode(goods.getGoodsCode());
				sku.setGoodsCode(goods.getGoodsCode());
				sku.setGoodsName(goods.getGoodsName());
				sku.setGoodsComponent(goods.getGoodsComponent());
				sku.setGoodsDisc(goods.getGoodsDisc());
				sku.setGoodsEffect(goods.getGoodsEffect());
				sku.setGoodsUnit(goods.getGoodsUnit());
				sku.setProducePlace(goods.getProducePlace());
				sku.setProduceCpy(goods.getProduceCpy());
				sku.setPic1(goods.getPic1());
				sku.setPic2(goods.getPic2());
				sku.setPic3(goods.getPic3());
				sku.setPic4(goods.getPic4());
				sku.setPic5(goods.getPic5());
				sku.setPic6(goods.getPic6());
				sku.setPic7(goods.getPic7());
				sku.setPic8(goods.getPic8());
				sku.setPic9(goods.getPic9());
				sku.setPic10(goods.getPic10());
				skuList.add(sku);

			}
			return R.ok(goodsSkuService.saveBatch(skuList));
		}
		return R.ok(goodsSkuService.save(null));

	}

}
